package com.empsys.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.auth.CredentialsProvider;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

/**
 * @author wzm
 * @create 2023-12-11 16:23
 * project_name:web2310
 * @Description
 */
@Component //注入对象到ioc容器中
public class AliyunOssUtils {
    //配置信息
    //1.硬编码
   /* String endpoint = "https://oss-cn-beijing.aliyuncs.com";//节点
    String accessKeyId = "LTAI5tCrrrEbpZsfkbMqzYpV";//AccessKey ID
    String accessKeySecret = "m50vxFldhYG20G0GpgSRsckXGhC0XO";//AccessKey Secret
    String bucketName = "web2310-pro";//Bucket名称*/

    //2.将配置信息抽取到properties中
    //四个参数 null springboot提供了获取外部属性的值的注解 @Value("${获取properties中的key}")
/*    @Value("${aliyun.oss.endpoint}")
    private String endpoint;
    @Value("${aliyun.oss.accessKeyId}")
    private String accessKeyId;
    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeySecret;
    @Value("${aliyun.oss.bucketName}")
    private String bucketName;*/
    //思考：如果有一个东西能一次性包裹所有属性且能一次从配置文件中读取



    //注入完整的一个包含了所有属性的bean
    @Autowired
    private AliOssBean aliOssBean;










    //阿里云文件上传的方法
    public String upload(MultipartFile multipartFile) throws IOException {
        //代码嵌入访问秘钥
        CredentialsProvider credentialsProvider = new DefaultCredentialProvider(aliOssBean.getAccessKeyId(), aliOssBean.getAccessKeySecret());

        //确定上传后文件的名字
        //获取原始文件名
        String originalFilename = multipartFile.getOriginalFilename();
        //上传文件的名字=uuid生成+原始文件名截取最后一个点后面的后缀名
        String newFileName = UUID.randomUUID().toString()+originalFilename.substring(originalFilename.lastIndexOf("."));

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(aliOssBean.getEndpoint(), credentialsProvider);

        //获取文件输入流
        InputStream inputStream = multipartFile.getInputStream();
       // 创建PutObjectRequest对象。
        PutObjectRequest putObjectRequest = new PutObjectRequest(aliOssBean.getBucketName(), newFileName, inputStream);
        // 创建PutObject请求。 上传文件
        ossClient.putObject(putObjectRequest);

        //节点前半+//++-bucketname+.+节点后半+/+文件名
        //图片路径  https://web2310-pro.oss-cn-beijing.aliyuncs.com/119.png
//        String[] split = endpoint.split("//");
        String url = aliOssBean.getEndpoint().split("//")[0] + "//" + aliOssBean.getBucketName() + "." + aliOssBean.getEndpoint().split("//")[1] + "/" + newFileName;

        //关闭客户端
        if (ossClient != null) {
            ossClient.shutdown();
        }
        return url;
    }
}
